import {MissonModel} from '@/mount/room/Misson/missonModel'
import { colorful } from '@/utils'
import global from '..'
/* 终端相关任务API集合 */

export default {
    /* 资源传送API */
    oterminal:{
        send(myRoom:string,disRoom:string,rType:ResourceConstant,num:number):string{
            var thisRoom = Game.rooms[myRoom]
            if (!thisRoom) return `[terminal] 找不到房间${thisRoom.name}!`
            var storage_ = Game.getObjectById(thisRoom.memory.TaskMemory.storageID as string) as StructureStorage
            var termial_ = Game.getObjectById(thisRoom.memory.TaskMemory.terminalID as string) as StructureTerminal
            if (!storage_ || !termial_ || storage_.store[rType] + termial_.store[rType] < num)  
                return `[terminal] 未找到相关建筑或没有足够资源!`
            var thisTask:RoomTask = {} as RoomTask
            Object.assign(thisTask,MissonModel['资源传送'])
            thisTask.relateCreep = ['manage01']
            thisTask.relateResource = [rType as ResourceConstant]
            var expense = Game.market.calcTransactionCost(num,myRoom,disRoom)
            thisTask.expense = expense
            thisTask.num = num
            thisTask.disRoom = [disRoom]
            if (thisRoom.mountMisson(thisTask))
                return `[terminal] 传送任务挂载成功\n\t房间${myRoom}->${disRoom} 资源:${rType},数量:${num},路费:${expense} !`
            else
                return `[terminal] 房间${myRoom} xxxx 任务挂载失败!`
        },
        Csend(myRoom:string):string{
            var thisRoom = Game.rooms[myRoom]
            if (!thisRoom) return `[terminal] 找不到房间${thisRoom.name}!`
            if (thisRoom.deleteMisson('CREEP','资源传送'))
                return `[terminal]房间${myRoom}资源传送任务删除成功!`
            else
                return `[termial]未能成功删除资源传送任务!`
        },
    },
    /* 市场行为API */
    omarket:{
        buy(roomName:string,rType:ResourceConstant,amount:number,price:number):number{
            return Game.market.createOrder({
                type: ORDER_BUY,
                resourceType: rType,
                price: price,
                totalAmount: amount,
                roomName: roomName   
            })
        },
        deal(roomName:string,id:string,amount:number):number{
            return Game.market.deal(id, amount, roomName);
        },
        // 查询订单
        look(rType:ResourceConstant,marType:"buy"|"sell")
        {
            var list = Game.market.getAllOrders({type: marType, resourceType: rType}); 
            var result = `当前市场上资源${rType}的${marType}订单如下:\n`
            for (var i of list)
            {
                result += `\tID:${i.id} 数量:${i.amount} 价格:${i.price} 坐标:${i.roomName} \n`
            }
            return result
        },
        /* 无脑购买资源 range是接受区间 */
        IamSB(roomName:string,rType:ResourceConstant,amount:number,range:number = 2):string{
            var list = Game.market.getAllOrders({type: 'sell', resourceType: rType});
            if (list.length <= 0) return `[market]未找到订单${rType} 的 sell 订单` 
            var HistoryList = Game.market.getHistory(rType)
            var allNum:number = 0
            for (var i of HistoryList)
            {
                allNum += i.avgPrice
            }
            let avePrice = allNum/HistoryList.length
            var order:Order
            for (var sb of list)
            {
                /* 可接受区间 */
                if(sb.amount >= amount && sb.price <= range + avePrice)
                {
                    if (!order) order = sb
                    else
                    {
                        if (order.price > sb.price)
                            order = sb
                    }
                }
            }
            if (!order) return `[market]未找到满足要求的订单： 资源 ${rType} 数量 ${amount} 价格区间${avePrice} -> ${avePrice+range}`
            else
            {
                if (Game.market.credits < order.price * amount) return colorful(`[market]咱们地主家也没余粮了!`,'red',true)
                var result =  Game.market.deal(order.id, amount, roomName);
                if (result == 0)   return colorful(`[market]交易成功! ${amount}数量的${rType}已经进入${roomName}的终端里!`,'green',true)
                else  return colorful(`[market]交易失败,${roomName}的终端可能正在忙!`,'red',true)
            }
        },
        ave(rType:ResourceConstant):string{
            var HistoryList = Game.market.getHistory(rType)
            var allNum:number = 0
            for (var i of HistoryList)
            {
                allNum += i.avgPrice
            }
            return `[market] 资源 ${rType} 14天来的平均价格为${allNum/HistoryList.length}`
        }
    }
}